package com.android.powerbankpad.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.android.powerbankpad.R;
import com.ws.universal.tools.utils.glide.GlideUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class RentResultDialog extends Dialog {
    private Context mContext;
    private TextView mTvDes1;
    private TextView mTvDes2;
    private ImageView mIvResult;

    public RentResultDialog(@NonNull Context context) {
        super(context, R.style.dialog_alarm);
        mContext = context;
        initUI();
    }

    public void setImage(int result, String des1, String des2, String imgUrl) {
        int resId = 0 == result ? R.mipmap.fail_image : R.mipmap.success_image;
       /* if (!TextUtils.isEmpty(des1)) {
            mTvDes1.setText(des1);
        } else {
            if(type==4){
                mTvDes1.setText(result == 0 ? "租借失败" : "租借成功");
            }else {
                mTvDes1.setText(result == 0 ? "归还失败" : "归还成功");
            }

        }*/
        mTvDes1.setText(des1);
        if (!TextUtils.isEmpty(des2)) {
            mTvDes2.setText(des2);
        }
        GlideUtils.CreateImageRound(imgUrl, mIvResult, resId, resId);
    }

    private void initUI() {
        View dlgView = LayoutInflater.from(mContext).inflate(R.layout.dialog_alarm_lease, null);

        mTvDes1 = dlgView.findViewById(R.id.mTvDes1);
        mTvDes2 = dlgView.findViewById(R.id.mTvDes2);
        mIvResult = dlgView.findViewById(R.id.mIvResult);

        //setContentView(dlgView);


//
        Window window = getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setGravity(Gravity.CENTER);
        window.setContentView(dlgView);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.FILL_PARENT;
        lp.height = WindowManager.LayoutParams.FILL_PARENT;
        window.setAttributes(lp);

        setCanceledOnTouch(false);

    }

    @Override
    public void show() {
        super.show();
        //自动关闭
        Observable.timer(5000L, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(Long aLong) {

                    }

                    @Override
                    public void onError(Throwable e) {
                        cancel();
                    }

                    @Override
                    public void onComplete() {
                        dismiss();
                    }
                });
    }

    public void setCanceledOnTouch(boolean isCanceledOnTouch) {
        setCanceledOnTouchOutside(isCanceledOnTouch);
    }


}
